#!/usr/bin/env python
#coding=utf-8
"""
sample ftp server
"""
import SocketServer, commands

class FtpServiceHandler(SocketServer.BaseRequestHandler):
	"""
	get req from client
	"""
	def handle(self):
		get_data = self.request.recv(1024).strip()
		print get_data
		cmd, filename = get_data.split()[:2]

		#对客户端上传的命令进行判断，不是get就直接抛异常
		if cmd != 'get':
			raise
		#本地对client传过来的文件名进行读文件操作
		buf = open(filename).read()
		if not buf:
			raise
		#读出文件的文件的所有内容，发送回client
		self.request.sendall('%s\t%s' % (len(buf), buf))

if __name__ == '__main__':
	host, port = '', 50010
	server = SocketServer.ForkingTCPServer((host, port), FtpServiceHandler)
	server.allow_reuse_address = True   #允许地址重用
	server.serve_forever()